package com.evan.review.launchmode;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.evan.review.R;

import java.util.Objects;

/**
 * 标准模式(standard)的Activity演示
 * <p>
 * 特点：
 * 1. 每次启动都会创建新的实例
 * 2. 可以有多个相同的Activity实例在同一个任务栈中
 */
public class StandardActivity extends AppCompatActivity {

    private static final String TAG = "StandardActivity";
    private TextView tvInfo;
    private int instanceCount = 0;
    private static int totalInstance = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_standard);

        // 记录实例数量
        instanceCount = ++totalInstance;

        initViews();
        updateInfo();

        Log.d(TAG, "onCreate: " + this.toString() + ", taskId: " + getTaskId() + ", instance: " + instanceCount);
    }

    private void initViews() {
        tvInfo = findViewById(R.id.tv_info);

        Button btnCreateSelf = findViewById(R.id.btn_create_self);
        btnCreateSelf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle extras = getIntent().getExtras();
                if (extras != null && extras.getBoolean("shouldLaunchSingleTop")) {
                    Intent intent = new Intent(StandardActivity.this, SingleTopActivity.class);
                    startActivity(intent);
                } else {
                    // 创建自身的新实例
                    Intent intent = new Intent(StandardActivity.this, StandardActivity.class);
                    startActivity(intent);
                }
            }
        });

        Button btnBack = findViewById(R.id.btn_back);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 返回上一个Activity
                finish();
            }
        });
    }

    private void updateInfo() {
        StringBuilder sb = new StringBuilder();
        sb.append("【标准模式演示】\n\n");
        sb.append("当前Activity: ").append(this.getClass().getSimpleName()).append("\n");
        sb.append("实例Hash: ").append(this.hashCode()).append("\n");
        sb.append("实例编号: ").append(instanceCount).append("/").append(totalInstance).append("\n");
        sb.append("任务栈ID: ").append(getTaskId()).append("\n\n");

        sb.append("特点说明:\n");
        sb.append("1. 标准模式下，每次启动Activity都会创建新的实例\n");
        sb.append("2. 当前实例编号与总实例数可以观察实例创建情况\n");
        sb.append("3. 点击\"创建自身新实例\"可以在同一任务栈创建多个实例\n");
        sb.append("4. 通过返回键可以按照压栈顺序的逆序返回\n\n");

        sb.append("日志查看提示:\n");
        sb.append("可在Logcat中过滤\"StandardActivity\"查看详细信息");

        tvInfo.setText(sb.toString());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.d(TAG, "onNewIntent: " + this.toString() + ", taskId: " + getTaskId() + ", instance: " + instanceCount);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: " + this.toString() + ", taskId: " + getTaskId() + ", instance: " + instanceCount);
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause: " + this.toString() + ", taskId: " + getTaskId() + ", instance: " + instanceCount);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: " + this.toString() + ", taskId: " + getTaskId() + ", instance: " + instanceCount);
    }
} 